<?php
session_start();
$title="แสดงข้อมูลลูกค้าทั้งหมด";
include ('header-admin.php');

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 20;
$limit_start = ($page-1)*$limit_end;

function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
    $output = "";
    $total = ceil($count/$limit);
    $navi_start = $page-$range;
    $navi_end = $page+$range;
    //test
     
    $send .= (!empty($send))? "&" : NULL;
         
    if($navi_start <= 0) $navi_start = 1;
    if($navi_end >= $total) $navi_end = $total;
	$type;
	if(isset($_GET['type'])){
		$type = $_GET['type'];
	}else{
		$type="1";
	}
     
    if($page>1){
        $navi_back = $page-1;
        if($page > 2)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&laquo;</strong></a> ";
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8249;</strong></a> ";
    }
    for($i = $navi_start; $i <= $navi_end; $i++){
        if($i == $page)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
        else
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
    }
    if($page < $total){
        $navi_next = $page+1;
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8250;</strong></a> ";
        if(($page+1) < $total)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&raquo;</strong></a>";
    }
    if($navi_start>$navi_end)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
    return $output;
}

$userID=$_SESSION["HBD_USER"];
$countTopic=0;
$search_cutomer = "";
if(isset ($_GET['search_cutomer']) && trim($_GET['search_cutomer']) != ''){
	$search_cutomer = $_GET['search_cutomer'];
}
if($userID == 1){
?>
<script language="javascript">
	function ConfirmChoice(id){
	answer = confirm("คุณแน่ใจว่าจะลบ ?")
		if (answer !=0)
			location = "doDeleteCustomer.php?id=" + id;
	}

</script>
<?}?>
<div id="center-block" class="main-content">
<img src="img/customer_icon.png"/ id="img-block-content">
<div id="content-title-right">
		<h2 class="caption-header">แสดงข้อมูลลูกค้า</h2>
		<span class="caption">แสดงข้อมูลลูกค้าทั้งหมด</span>
		<span class="right-button">
		   <form name="jump" method="GET" action="#">
		   			<input type="text" id="search_cutomer" name="search_cutomer" value="<?=$search_cutomer?>" size="15"/>
		   			<input type="submit" value="ค้นหา" />
		   </form>
				</span>
	</div>

	<hr>
	<table summary="webboard about Datastrucutre learning" id="table">
		<thead>
		<tr>
			<th scope="col" abbr="reserv_id">ID</th>
			<th scope="col" abbr="reserv_id">เลขบัตรประชาชน</th>
			<th scope="col" abbr="reserv_begin">ชื่อ</th>
			<th scope="col" abbr="reserv_begin">นามสกุล</th>		
			<th scope="col" abbr="reserv_end">E-mail</th>
			<th scope="col" abbr="room_id">โทรศัพท์</th>
			<th scope="col" >จัดการ</th>
		</tr>	
		</thead>
		<tbody>
		<?
	$sql="SELECT * FROM `customer` ";
	$flag = false;
	if(isset ($_GET['search_cutomer']) && trim($_GET['search_cutomer']) != ''){
		$flag = true;
		$where = "WHERE customer_id LIKE '%".$search_cutomer."%' OR customer_name LIKE '%".$search_cutomer."%' OR customer_surname LIKE '%".$search_cutomer."%' OR customer_email LIKE '%".$search_cutomer."%' OR customer_tel LIKE '%".$search_cutomer."%' ";
		$sql .= $where;
	}
	$order = "ORDER BY customer_name DESC";
	$sql .= $order;
	$result = mysql_query($sql. " LIMIT " . $limit_start. "," . $limit_end);
	$count = mysql_num_rows($result);
	$total = mysql_num_rows(mysql_query($sql));
	$countTopic=0;
	if($total > 0){
	while ($rs = mysql_fetch_array($result)){
		if($countTopic%2==0)
			echo '<tr class="odd">';
		else
			echo '<tr class="even">';
		echo '<td class="customer">' . $rs['id'] . '</td>';
		echo '<td class="customer">' . str_ireplace($search_cutomer,"<b style='color:red;'>".$search_cutomer."</b>",$rs['customer_id']) . '</td>';
		echo '<td class="customer">' . str_ireplace($search_cutomer,"<b style='color:red;'>".$search_cutomer."</b>",$rs['customer_name'] ). '</td>';
		echo '<td class="customer">' . str_ireplace($search_cutomer,"<b style='color:red;'>".$search_cutomer."</b>",$rs['customer_surname']) . '</td>';
		echo '<td class="customer">' . str_ireplace($search_cutomer,"<b style='color:red;'>".$search_cutomer."</b>",$rs['customer_email'])  . '</td>';
		echo '<td class="customer">' . str_ireplace($search_cutomer,"<b style='color:red;'>".$search_cutomer."</b>",$rs['customer_tel'])  . '</td>';
		echo '<td ><a href="editCustomer.php?id=' . $rs['id'] . '"><img src="img/icon_edit.png" /></a>&nbsp;<a href="#" onclick="ConfirmChoice(\'' . $rs['id'] . '\'); return false;"><img src="img/icon_delete.png" /></a></td></tr>';
		$countTopic++;
	}		
	echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, null) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
	?>
		</tbody>

	</table>
	</div>

<?
include ('footer.php');
?>